package cn.shuto.liferay.plugin.chart.service.service.impl;

import java.util.Date;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;

import cn.shuto.liferay.plugin.chart.service.model.DataSourceCfg;
import cn.shuto.liferay.plugin.chart.service.service.base.DataSourceCfgLocalServiceBaseImpl;

/**
 * The implementation of the data source cfg local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link cn.shuto.liferay.plugin.chart.service.service.DataSourceCfgLocalService}
 * interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 *
 * @author shixw
 * @see cn.shuto.liferay.plugin.chart.service.service.base.DataSourceCfgLocalServiceBaseImpl
 * @see cn.shuto.liferay.plugin.chart.service.service.DataSourceCfgLocalServiceUtil
 */
public class DataSourceCfgLocalServiceImpl extends
		DataSourceCfgLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * cn.shuto.liferay
	 * .plugin.chart.service.service.DataSourceCfgLocalServiceUtil} to access
	 * the data source cfg local service.
	 */

	public DataSourceCfg addDataSourceCfg(String name, String description,
			String driver, String connUrl, String dbusername,
			String dbpassword, ServiceContext serviceContext)
			throws SystemException, PortalException {

		long groupId = serviceContext.getScopeGroupId();
		long userId = serviceContext.getUserId();
		User user = userPersistence.findByPrimaryKey(userId);
		Date now = new Date();

		long dataSourceCfgId = counterLocalService.increment();
		DataSourceCfg dataSourceCfg = dataSourceCfgPersistence
				.create(dataSourceCfgId);
		dataSourceCfg.setUuid(serviceContext.getUuid());
		dataSourceCfg.setUserId(userId);
		dataSourceCfg.setGroupId(groupId);
		dataSourceCfg.setCompanyId(user.getCompanyId());
		dataSourceCfg.setUserName(user.getFullName());
		dataSourceCfg.setCreateDate(serviceContext.getCreateDate(now));
		dataSourceCfg.setModifiedDate(serviceContext.getModifiedDate(now));

		dataSourceCfg.setName(name);
		dataSourceCfg.setDescription(description);
		dataSourceCfg.setDriver(driver);
		dataSourceCfg.setConnUrl(connUrl);
		dataSourceCfg.setDbusername(dbusername);
		dataSourceCfg.setDbpassword(dbpassword);

		dataSourceCfgPersistence.update(dataSourceCfg);
		return dataSourceCfg;
	}

	public DataSourceCfg updateDataSourceCfg(long dataSourceCfgId, String name,
			String description, String driver, String connUrl,
			String dbusername, String dbpassword, ServiceContext serviceContext)
			throws SystemException, PortalException {

		long groupId = serviceContext.getScopeGroupId();
		long userId = serviceContext.getUserId();
		User user = userPersistence.findByPrimaryKey(userId);
		Date now = new Date();

		DataSourceCfg dataSourceCfg = getDataSourceCfg(dataSourceCfgId);
		dataSourceCfg.setUserId(userId);
		dataSourceCfg.setGroupId(groupId);
		dataSourceCfg.setCompanyId(user.getCompanyId());
		dataSourceCfg.setUserName(user.getFullName());
		dataSourceCfg.setModifiedDate(serviceContext.getModifiedDate(now));

		dataSourceCfg.setName(name);
		dataSourceCfg.setDescription(description);
		dataSourceCfg.setDriver(driver);
		dataSourceCfg.setConnUrl(connUrl);
		dataSourceCfg.setDbusername(dbusername);
		dataSourceCfg.setDbpassword(dbpassword);

		dataSourceCfgPersistence.update(dataSourceCfg);
		return dataSourceCfg;
	}
}
